package com.example.a14874.demoa;

import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public class ContentProvider extends android.content.ContentProvider {
    private StuOpenHelper openHelper;
    private static final String AUTOHORITY="com.gdcp.student";
    private static UriMatcher uriMatcher=new UriMatcher( UriMatcher.NO_MATCH );
    private SQLiteDatabase db;

    static {
        uriMatcher.addURI(AUTOHORITY, "student", 1);
        uriMatcher.addURI(AUTOHORITY, "score", 2);
    }

    @Override
    public boolean onCreate() {
       openHelper=new StuOpenHelper( getContext(),"stu.db",null,1 );
       return true;
    }

    @Nullable
    @Override
    public Cursor query( @NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String orderBy ) {
       db = openHelper.getReadableDatabase();
        switch (uriMatcher.match(uri)){
            case 1:
                Cursor cursor1 = db.query("student", projection, selection,selectionArgs,null, null, orderBy);
                return cursor1;

            case 2:
                Cursor cursor2 = db.query("score", projection, selection,selectionArgs,null, null, orderBy);
                return cursor2;

        }
        return null;
    }

    @Nullable
    @Override
    public String getType( @NonNull Uri uri ) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert( @NonNull Uri uri, @Nullable ContentValues contentValues ) {
       db = openHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)){
            case 1:
                db.insert("student", null, contentValues);
                break;

            case 2:
               db.insert("score", null, contentValues);
                break;
        }

        db.close();
        return null;
    }


    @Override
    public int delete( @NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs ) {
        switch (uriMatcher.match(uri)){
            case 1:
                db = openHelper.getWritableDatabase();
                int rows =db.delete("student", selection, selectionArgs);
                db.close();
                return rows;
            case 2:
                break;
        }
        return 0;
    }

    @Override
    public int update( @NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String selection, @Nullable String[] selectionArgs ) {
        switch (uriMatcher.match(uri)){
            case 1:
               db = openHelper.getWritableDatabase();
                int rows = db.update("student", contentValues, selection, selectionArgs);
               db.close();
                return rows;

            case 2:
                break;
        }


        return 0;
    }
}
